package com.example.hotHub.scheduled;

import com.example.hotHub.cnst.CommonConst;
import com.example.hotHub.model.NormalHotSearch;
import com.example.hotHub.service.HotSearchWrapperService;
import com.example.hotHub.utils.FileUtils;
import com.example.hotHub.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;

@Slf4j
@Component
@EnableScheduling
public class HotPointScheduled {

    @Autowired
    @Qualifier("threadPoolExecutor")
    private ExecutorService executorService;

    @Autowired
    private HotSearchWrapperService hotSearchWrapperService;

    @Autowired
    private RedisUtil redisUtil;

    @Scheduled(fixedDelay = 5 * 60 * 1000)
    public void getHotPointTask() {
        this.getHotPoint();
    }

    public void getHotPoint() {

        log.info("缓存热点");

        executorService.execute(
                () -> {
                    List<NormalHotSearch> hotSearchList = hotSearchWrapperService.getBaiduHotSearchList();
                    synchronized (hotSearchWrapperService) {
                        redisUtil.set("baidu", hotSearchList);
                    }
                    log.info("缓存百度热点");
                }
        );

        executorService.execute(
                () -> {
                    List<NormalHotSearch> hotSearchList = hotSearchWrapperService.getWeiboHotSearchList();
                    synchronized (hotSearchWrapperService) {
                        redisUtil.set("weibo", hotSearchList);
                    }
                    log.info("缓存微博热点");
                }
        );

        executorService.execute(
                () -> {
                    List<NormalHotSearch> hotSearchList = hotSearchWrapperService.getZhihuHotSearchList();
                    synchronized (hotSearchWrapperService) {
                        redisUtil.set("zhihu", hotSearchList);
                    }
                    log.info("缓存知乎热点");
                }
        );

        executorService.execute(
                () -> {
                    List<NormalHotSearch> hotSearchList = hotSearchWrapperService.getToutiaoHotSearchList();
                    synchronized (hotSearchWrapperService) {
                        redisUtil.set("toutiao", hotSearchList);
                    }
                    log.info("缓存头条热点");
                }
        );

        executorService.execute(
                () -> {
                    List<NormalHotSearch> hotSearchList = hotSearchWrapperService.getTencentHotSearchList();
                    synchronized (hotSearchWrapperService) {
                        redisUtil.set("tencent", hotSearchList);
                    }
                    log.info("缓存腾讯热点");
                }
        );



        executorService.execute(
                () -> {
                    List<NormalHotSearch> hotSearchList = hotSearchWrapperService.getHaokanHotSearchList();
                    synchronized (hotSearchWrapperService) {
                        redisUtil.set("haokan", hotSearchList);
                    }
                    log.info("缓存好看视频热点");
                }
        );

    }

    @Scheduled(fixedDelay = 12 * 60 * 60 * 1000)
    public void bili() {
        executorService.execute(
                () -> {
                    List<NormalHotSearch> hotSearchList = hotSearchWrapperService.getBiliHotSearchList("all");
                    synchronized (hotSearchWrapperService) {
                        redisUtil.set("bili", hotSearchList);
                    }
                    List<String> videoNameList = hotSearchList.stream().map(NormalHotSearch::getVideoName).collect(Collectors.toList());
//                    String dirPath = this.getClass().getClassLoader().getResource("static").getPath() + "/video/bili";
                    String dirPath = CommonConst.USER_ROOT_HOME + "/video/bili";
                    FileUtils.removeFile(dirPath, videoNameList);
                    log.info("缓存BiliBili热点");
                }
        );
    }


}
